package com.xjrsoft.module.interfaceManager.utils;

import com.xjrsoft.core.tool.node.INode;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.module.interfaceManager.vo.CategoryTreeVo;
import com.xjrsoft.module.interfaceManager.vo.InfoTreeListVo;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public final class InterfaceUtil {
    private InterfaceUtil(){}

    public static Set<String> getResultCategoryIdList(List<CategoryTreeVo> resultList) {
        Set<String> idList = new LinkedHashSet<>();
        if (CollectionUtil.isNotEmpty(resultList)) {
            for (CategoryTreeVo categoryTreeVo : resultList) {
                existInterfaceInfo(idList, null, categoryTreeVo);
            }
        }
        return idList;
    }

    private static void existInterfaceInfo(Set<String> idList, List<String> tempIdList, INode categoryTreeVo) {
        List<INode> children = categoryTreeVo.getChildren();
        if (CollectionUtil.isEmpty(children)) {
            return;
        }
        if (tempIdList == null) tempIdList = new ArrayList<>();
        tempIdList.add(categoryTreeVo.getId());
        if (children.get(0) instanceof InfoTreeListVo) {
            idList.addAll(tempIdList);
        }
        for (INode node : children) {
            existInterfaceInfo(idList, tempIdList, node);
        }
    }
}
